package com.example.personalaccount.service.impl;

import com.example.personalaccount.dto.*;
import com.example.personalaccount.entity.Account;
import com.example.personalaccount.entity.User;

import com.example.personalaccount.entity.UserInform;
import com.example.personalaccount.repository.AccountRepository;
import com.example.personalaccount.repository.UserInformRepository;
import com.example.personalaccount.repository.UserRepository;
import com.example.personalaccount.service.AccountService;
import com.example.personalaccount.service.UserService;
import com.example.personalaccount.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;


@Service("authService")
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInformRepository userInformRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder ;

    @Autowired
    private JwtService jwtService;

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public JwtResponse register(RegisterRequest request) {
        if(userRepository.getUserByUsername(request.getUsername()) == null) {
            User user = new User();
            user.setUsername(request.getUsername());
            user.setPassword(passwordEncoder.encode(request.getPassword()));
            user.setRole("ROLE_USER");
            user.setTelephone(request.getTelephone());
            User save = userRepository.save(user);
            accountRepository.save(new Account(null, user.getId(), new ArrayList<>(), new HashMap<>()));
            userInformRepository.save(new UserInform(null,user.getId(),"user_"+user.getId(),"空空如也",null,false));
            if (save != null) {
                return getAuthResponse(user,userInformRepository.getUserInformByUserId(user.getId()));
            }
            throw  new IllegalStateException("register failed");
        }
        else throw  new IllegalStateException("User already registered");
    }  // 注册 后面加个 邮箱认证 就行

    @Override
    public JwtResponse login(JwtRequest request) {
        User user = userRepository.getUserByUsername(request.getUsername());
        if (Objects.isNull(user))
            throw new RuntimeException("用户或密码错误");
        if (passwordEncoder.matches(request.getPassword(), user.getPassword()))
        {
            if (user.getRole().equals("ROLE_ADMIN") || user.getRole().equals("ROLE_USER"))
            return  getAuthResponse(user,userInformRepository.getUserInformByUserId(user.getId()));
            throw new RuntimeException("请联系管理员申诉，当前您已被封禁");
        }
        throw new RuntimeException("用户或密码错误");
    }

    @Override
    public Boolean changePassword(JwtRequest request, String password) {
        User user = userRepository.getUserByUsername(request.getUsername());
        if (passwordEncoder.matches(request.getPassword(), user.getPassword()))
        {
            user.setPassword(passwordEncoder.encode(password));
            userRepository.save(user);
            return true ;
        }
        return false;
    }

    @Override
    public Boolean recoverCode(long id) {
        User user = userRepository.getUserById(id);
        if (Objects.isNull(user))
            throw new RuntimeException("please check the user is registered");
        user.setPassword(passwordEncoder.encode("password"));
        userRepository.save(user);
        return true;
    }

    private JwtResponse getAuthResponse(User user,UserInform userInform) {
        return new JwtResponse(jwtService.generateToken(user),user.getId(),userInform.getUsername(),user.getRole(),userInform.getImgId());
    }
}
